using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace Microsoft.Tools.CodeRunner.UnitTest 
{
	[TestClass]
	public class CommandTests 
	{
		[ClassInitialize]
		public static void ClassCleanup(TestContext context)
		{
		}
		
		[TestMethod]
		public void TestNoCapture() 
		{
			Assert.IsTrue(Command.Run("dir /b") == 0);
		}

		[TestMethod]
		[DeploymentItem(@"UnitTest\TestSource\CommandTest.csr")]
		public void TestCaptureOutput() 
		{
			string output;
			
			Assert.IsTrue(Command.Run("csr.exe CommandTest.csr /1", out output) == 0);
			Assert.IsTrue(output == "one argument\r\n");
		}

		[TestMethod]
		public void TestCaptureOutputAndError() 
		{
			string output;
			string error;
			
			Assert.IsTrue(Command.Run("csr.exe CommandTest.csr /1 /2", out output, out error) == 0);
			Assert.IsTrue(output == "two arguments\r\n");
			Assert.IsTrue(error == "error text\r\n");
		}

		[TestMethod]
		public void TestQuotedContent() 
		{
			string output;
			
			Assert.IsTrue(Command.Run("csr.exe CommandTest.csr /1 /2 \"plus quoted string\"", out output) == 0);
			Assert.IsTrue(output == "three arguments plus quoted string\r\n");
		}

		[TestMethod]
		public void TestNonZeroExitCode()
		{
			string output;

			Assert.IsTrue(Command.Run("csr.exe CommandTest.csr /1 /2 /3 /4 /5", out output) != 0);
		}

		[TestMethod]
		public void TestDebugMode()
		{
			string output;

			Command.DebugMode = true;
			Assert.IsTrue(Command.DebugMode);
			Assert.AreEqual<int>(Command.Run("csr.exe Dummy.csr", out output), 0);
			Assert.AreEqual(output, "csr.exe Dummy.csr\r\n");
		}
		
		[TestCleanup]
		public void TestCleanup()
		{
			Command.DebugMode = false;
		}
	}
}
